<template>
	<view class="searchPage">
		<!-- <u-navbar v-if="showHeader" title="搜索" :autoBack="true"></u-navbar> -->
		
		<view class="search" >
			<!-- <image src="../../static/images/whiteBack.png" mode="widthFix" class="backIcon" @click="back"></image> -->
			<u-search height="37" bgColor="#fff" shape="round" :showAction="false"  @search="getData"></u-search>
			<!-- <view class="fabu" @click="showreleaseDia">
				<image src="@/static/images/fabu_icon.png" mode="widthFix"></image>
				<span>发布</span>
			</view> -->
		</view>
		
		<view class="content" v-if="listData.length > 0">
			<view class="card" v-for="(item, index) in listData" :key="index">
				<new-item :isMylife="false" :zhuiyi="true" :new="item" :newIndex="index" @share="showShareDia" @delOver="getData" :ispinglun="true"></new-item>
			</view>
		</view>
		<nodata v-if="listData.length == 0 && loadingOver"></nodata>
		<view class="nomore" v-if="query.lastDocId == '' && listData.length > 0">
			没有更多了 ~
		</view>
		
		<u-popup :show="releaseDia" round="20" @close="releaseDia = false">
			<release ref="release" @closeDia="releaseDia = false" @toDraft="toDraft" :isMylife="false"></release>
		</u-popup>
		
		<uni-popup ref="popup" type="bottom">
			<share @closeDia="$refs.popup.close()"></share>
		</uni-popup>
	</view>
</template>

<script>
	import newItem from '@/components/new-item.vue';
	import release from '@/components/release.vue';
	import share from '@/components/share/share.vue'
export default {
	components: {  newItem,release,share},
	data() {
		return {
			loadingOver:false,
			query: {
				lastDocId:'',
			},
			listData: [],
			listOver:false,
			isMylife:'',
			showHeader:true,
			releaseDia:false,
			familyMemberId:'',
			familyId:'',
			checkShareItem:{},
			safeAreaInsets:{}
		};
	},
	onPullDownRefresh() {
		this.query.lastDocId = ''
		this.getData()
	},
	onReachBottom() {
		if(!this.listOver){
			this.getData();
		}
	},
	onLoad(e) {
		this.query = JSON.parse(e.queryData);
		console.log(this.query)
		if(e.isMylife){
			this.isMylife = e.isMylife
			this.getData2();
		}else{
			this.getData();
		}
		
		
	
	},
	methods: {
		toDraft(){
			uni.navigateTo({
				url: '/pages/draft/index?isMylife=1'
			});
		},
		showreleaseDia(){
			this.releaseDia = true
			this.$nextTick(()=>{
				this.$refs.release.init(this.familyMemberId,this.familyId)
			})
		},
		initSearch(query){
			this.query = query
			this.showHeader = false
			this.familyMemberId = query.familyAttenders[0]
			this.familyId = query.familyId
			this.query.username = query.username
			
			const {
				safeAreaInsets
			} = uni.getSystemInfoSync()
			this.safeAreaInsets = safeAreaInsets
			console.log(safeAreaInsets)
			
			
			this.getData();
		},
		getData2() {
			uni.showLoading({
				title:'加载中...'
			})
			this.loadingOver = false
			this.$api.queryLives(this.query).then(res => {
				console.log(res);
		
				if (res.lastDocId) {
					this.query.lastDocId = res.lastDocId;
					this.listData = [...this.listData, ...res.list];
				}else{
					this.query.lastDocId = ''
					this.listData = res.list;
				}
				
				
				if(res.list.length < this.query.pageSize){
					this.listOver = true
				}
				this.loadingOver = true
				uni.hideLoading()
				uni.stopPullDownRefresh();
			});
		},
		getData(e) {
			uni.showLoading({
				title:'加载中...'
			})
			this.loadingOver = false
			if(e){
				this.query.searchContent = e;
			}else{
				this.query.searchContent = '';
			}
			this.$api.queryArticles(this.query).then(res => {
				console.log(res);

				if (res.lastDocId) {
					this.query.lastDocId = res.lastDocId;
					this.listData = [...this.listData, ...res.list];
				}else{
					this.query.lastDocId = ''
					this.listData = res.list;
				}
				
				
				if(res.list.length < this.query.pageSize){
					this.listOver = true
				}
				this.loadingOver = true
				uni.hideLoading()
				uni.stopPullDownRefresh();
			});
		},
		showShareDia(item) {
			this.checkShareItem = item
			this.$refs.popup.open('bottom')
		},
		// 分享到朋友圈
		onShareTimeline() {
			let id = this.checkShareItem.docId;
			let that = this
			let data = {
				title: that.checkShareItem.title,
				path: `/pages/news/details?id=${id}&showHome=1`,
				imageUrl: that.checkShareItem.posterUrl,
			}
			return data
		},
		// 分享给好友
		onShareAppMessage(options) {
		
			var that = this;
			let id = that.checkShareItem.docId;
			console.log(that.checkShareItem)
			var shareObj = {
				title: that.checkShareItem.title,
				desc: '',
				path: `/pages/news/details?id=${id}&showHome=1`,
				imageUrl: that.checkShareItem.posterUrl,
			}
		
			return shareObj;
		},
	}
};
</script>

<style lang="scss" scoped>
	.searchPage{
		background-color: #f8f8f8;
		height: 100%;
		.card{
			margin-top: 20rpx;
		}
	}
	
	.search {
		background-color: #de4849;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10rpx 30rpx 30rpx 30rpx;
		padding-top: 10rpx;
		padding-bottom: 20rpx;
		width: 100%;
		.fabu {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			margin-left: 30rpx;
			image {
				width: 50rpx;
				height: 50rpx;
			}
			span {
				font-size: 25rpx;
				color: #fff;
			}
		}
	}
</style>
